package util

// app/errors/business_error.go

import "fmt"

type BusinessError struct {
	StatusCode int
	Message    string
	Err        error
}

func (e *BusinessError) Error() string {
	if e.Err != nil {
		return fmt.Sprintf("%s: %v", e.Message, e.Err)
	}
	return e.Message
}

func NewBusinessError(statusCode int, message string) *BusinessError {
	return &BusinessError{
		StatusCode: statusCode,
		Message:    message,
	}
}

func WrapBusinessError(statusCode int, message string, err error) *BusinessError {
	return &BusinessError{
		StatusCode: statusCode,
		Message:    message,
		Err:        err,
	}
}

// 常用错误定义
var (
	ErrNotFound     = NewBusinessError(404, "资源不存在")
	ErrUnauthorized = NewBusinessError(401, "未授权访问")
	ErrBadRequest   = NewBusinessError(400, "请求参数错误")
	ErrBadSearch    = NewBusinessError(400, "查询失败")
)
